/*
 * The path to the logging stylesheet, change this if your javascript
 * and css files are in different locations
 */
var STYLE_SHEET_LOCATION = "./logStyle.css";

var LOG_LEVEL = -1;

var LOG_LEVEL_TRACE=0;
var LOG_LEVEL_DEBUG=1;
var LOG_LEVEL_INFO=2;
var LOG_LEVEL_WARN=3;
var LOG_LEVEL_ERROR=4;
var LOG_LEVEL_FATAL=5;
var LOG_LEVEL_NONE=6;

//Mapping to the string representations of different log levels
var LOG_LEVELS = new Array("TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "NONE");

//Holds the log window
var LOG_WINDOW = null;


/*
 * Initializes the logger, with the optional logLevel parameters
 */
function initializeLogger(logLevel) {
	if (logLevel == null) {
		logLevel = LOG_LEVEL_TRACE;
	}
	LOG_LEVEL = logLevel;
}

/*
 * Clears the console
 */
function clearConsole() {
	openWindow();
}

/*
 * Log at the TRACE level
 */
function trace(logString) {
	log(logString, LOG_LEVEL_TRACE);
}

/*
 * Log at the DEBUG level
 */
function debug(logString) {
	//alert("called debug");
	log(logString, LOG_LEVEL_DEBUG);
}

/*
 * Log at the INFO level
 */
function info(logString) {
	log(logString, LOG_LEVEL_INFO);
}

/*
 * Log at the WARN level
 */
function warn(logString) {
	log(logString, LOG_LEVEL_WARN);
}

/*
 * Log at the ERROR level
 */
function error(logString) {
	log(logString, LOG_LEVEL_ERROR);
}

/*
 * Log at the FATAL level
 */
function fatal(logString) {
	log(logString, LOG_LEVEL_FATAL);
}

/*
 * Opens a new window and writes the default HTML to it.
 */
function openWindow() {
	LOG_WINDOW = window.open("", "win",'height=800,width=600, scrollbars=yes, resizable');
	LOG_WINDOW.moveTo(0, 0);
	LOG_WINDOW.document.close();
	LOG_WINDOW.document.open("text/html", "replace");
    var html = "<html><head><title>Log Console</title><LINK href=\"" + STYLE_SHEET_LOCATION + "\" rel=\"stylesheet\" type=\"text/css\"></head><body id=\"logbody\"><div id=\"divver\" class=\"logDEBUG\">Log Ouput:</div>";
	LOG_WINDOW.document.write(html);
	//doc.close();

	//I don't know why I have to have this alert here, logging doesn't work without it!
	alert("initialized logging");
	
}

/*
 * Write out the log string to the console window
 */
function log(logString, logLevel) {
	
	//Only log to console if logger has been initialized (greater than -1)
	if (LOG_LEVEL > -1) {
		
		//If the log window got closed, reopen it
		if (LOG_WINDOW == null || LOG_WINDOW.closed) {
			openWindow();
		}

		//alert("logLevel: " + logLevel + " LOG_LEVEL: " + LOG_LEVEL);
		if (logLevel >= LOG_LEVEL) {
			var date = new Date();
			var htmlString = LOG_LEVELS[logLevel] + "(" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + ":" +  date.getMilliseconds() + "): " + logString;
			var style = "log"+LOG_LEVELS[logLevel];
			
			LOG_WINDOW.document.write("<div class=" + style + ">" + htmlString + "</div>");
		} else {
		}
	}
}